package cn.calm.xhtml.impl.ui;

import cn.calm.xhtml.annotation.Attribute;
import cn.calm.xhtml.annotation.Tag;
import cn.calm.xhtml.api.exception.XhtmlException;
import cn.calm.xhtml.api.service.ResponseWriter;
import cn.calm.xhtml.api.service.XhtmlContext;
@Tag(namespace = "http://www.calm.cn/xhtml/extras/ui", attributes = {
		@Attribute(name = "label", description = "标签", template = "<label></label><input name=\"label\"/>"),
		@Attribute(name = "checked", description = "是否选中", template = "<label></label><input name=\"checked\"/>"),
		@Attribute(name = "value", description = "提交值", template = "<label></label><input name=\"value\"/>"),
		@Attribute(name = "name", description = "提交名称", template = "<label></label><input name=\"name\"/>") })
public class CheckBox extends InputNode {
	public static final String KEYS_CHECKED = "checked";

	public static final String KEYS_LABEL = "label";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	public void processNode(XhtmlContext context) throws XhtmlException {
		ResponseWriter responseWriter = context.getResponseWriter();
		Object labelObj = getAttrValue(KEYS_LABEL, context);
		if (labelObj != null) {
			responseWriter.startTag("label");
			responseWriter.write(">");
		}
		responseWriter.startTag("input");
		responseWriter.writeAttr("type", "checkbox");
		writeAttr(context, "value", KEYS_VALUE);
		writeAttr(context, "name", KEYS_NAME);
		
		Object valueObj = getAttrValue(KEYS_CHECKED, context);
		if (valueObj != null) {
			Boolean b = Boolean.valueOf(valueObj.toString());
			if (b) {
				responseWriter.writeAttr("checked", "checked");
			}
		}
		responseWriter.write(" />");
		if (labelObj != null) {
			responseWriter.write(labelObj);
			responseWriter.endTag("label");
		}
	}
}
